package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Account;
import com.service.AccountService;
import com.util.ConstUtils;
import com.vo.AccountVo;
import com.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/27 - 22:40
 */
@RestController
@RequestMapping("/account")
@RolesAllowed({"ROLE_ADMIN", "ROLE_SUPERADMIN"})
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 根据条件查询account列表
     *
     * @param accountVo
     * @return
     */
    @RequestMapping("/list")
    public DataVO findAccountList(AccountVo accountVo) {
        PageHelper.startPage(accountVo.getPage(), accountVo.getLimit());
        PageInfo<Account> pageInfo = new PageInfo<Account>(accountService.findAccountList(accountVo));
        return new DataVO<Account>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 修改account
     *
     * @param account
     * @return
     */
    @RequestMapping("/update")
    public Map<String, Object> updateAccount(Account account) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (accountService.updateAccount(account) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    /**
     * 删除account
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/del")
    public Map<String, Object> deleteAccount(Integer accountId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (accountService.deleteAccount(accountId) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }
}
